﻿#region header

//  ***************************************************************************
// *                                LICENSE
//  ***************************************************************************
//  * "Haplous" .NET Simple 2D Game Framework
//  * 
//  * Copyright © Jonathan Evans 2014-2014
//  * 
//  * This program is free software; you can redistribute it and/or
//  * modify it under the terms of the GNU General Public License
//  * as published by the Free Software Foundation; either version 2
//  * of the License, or (at your option) any later version.
//  * 
//  * This program is distributed in the hope that it will be useful,
//  * but WITHOUT ANY WARRANTY; without even the implied warranty of
//  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  * GNU General Public License for more details.
//  * 
//  * You should have received a copy of the GNU General Public License
//  * along with this program; if not, write to the Free Software
//  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
//  * 
//  ***************************************************************************
//  * 
//  * Contact: opencollar@googlemail.com
//  * 
//  * Website: http://haplous.codeplex.com/
//  *
//  * NuGet:   http://www.nuget.org/packages/haplous/
//  *
//  * File:    Haplous/Haplous/TextBlock.cs
//  *
//  ***************************************************************************

#endregion

using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;

using Haplous.Correctness;

using JetBrains.Annotations;

namespace Haplous
{
    /// <summary>
    ///     The basic class for all visual elements in a game that have size and position.
    /// </summary>
    [SuppressMessage("Microsoft.Design", "CA1063:ImplementIDisposableCorrectly")]
    [DebuggerDisplay("Solid: Name={Name}; Position={_position.X),{_position.Y); Size={_size.Width),{_size.Height); Text={Text}")]
    public class TextBlock : Block, ITextBlock
    {
        /// <summary>
        ///     The foreground color of the text element.
        /// </summary>
        [NotNull]
        private readonly Color _foreground = new Color();

        /// <summary>
        ///     The font family to use when displaying the text.
        /// </summary>
        [NotNull]
        private string _fontFamily = "Tahoma";

        /// <summary>
        ///     The font size, in pixels.
        /// </summary>
        private double _fontSize = 12;

        /// <summary>
        ///     The horizontal alignment of the text within the text block.
        /// </summary>
        private HorizontalAlignment _horizontalAlignment;

        /// <summary>
        ///     The text to display.
        /// </summary>
        [CanBeNull]
        private string _text;

        /// <summary>
        ///     The vertical alignment of the text within the text block.
        /// </summary>
        private VerticalAlignment _verticalAlignment;

        /// <summary>
        ///     Initializes a new instance of the <see cref="TextBlock" /> class.
        /// </summary>
        /// <param name="game">
        ///     The game to which the class belongs.  The <paramref name="game" /> must not be
        ///     <see langword="null" />.
        /// </param>
        /// <param name="name">A name that uniquely identifies this text block.</param>
        public TextBlock([NotNull] Game game, [NotNull] string name) : base(game, name)
        {}

        /// <summary>
        ///     The text to display.
        /// </summary>
        [CanBeNull]
        public string Text
        {
            get { return _text; }
            set { OnPropertyChanged("Text", ref _text, value); }
        }

        /// <summary>
        ///     The font size, in pixels.
        /// </summary>
        /// <value>The size of the font.</value>
        /// <exception cref="System.ArgumentOutOfRangeException"><see cref="FontSize" /> must be at least 4 pixels.</exception>
        /// <remarks>Measured in pixels.  The minimum size permitted is 4 pixels.</remarks>
        public double FontSize
        {
            get { return _fontSize; }
            set
            {
                if(value <= 4)
                    throw new ArgumentOutOfRangeException("value", value, "'FontSize' must be at least 4 pixels.");

                OnPropertyChanged("FontSize", ref _fontSize, value);
            }
        }

        //TODO: Make these properties mode properties?

        /// <summary>
        ///     The font family to use when display the text.
        /// </summary>
        [NotNull]
        public string FontFamily
        {
            get { return _fontFamily; }
            set
            {
                value.ValidateArgument("value", StringMustNotBe.NullOrEmptyOrWhiteSpace);

                OnPropertyChanged("FontFamily", ref _fontFamily, value);
            }
        }

        /// <summary>
        ///     Gets the foreground color.
        /// </summary>
        /// <value>The foreground color.</value>
        public Color Foreground
        {
            get { return _foreground; }
        }

        /// <summary>
        ///     Gets or sets the vertical alignment of the text within the text block.
        /// </summary>
        /// <value>The vertical alignment.</value>
        public VerticalAlignment VerticalAlignment
        {
            get { return _verticalAlignment; }
            set { OnPropertyChanged("VerticalAlignment", ref _verticalAlignment, value); }
        }

        /// <summary>
        ///     Gets or sets the horizontal alignment of the text within the text block.
        /// </summary>
        /// <value>The horizontal alignment.</value>
        public HorizontalAlignment HorizontalAlignment
        {
            get { return _horizontalAlignment; }
            set { OnPropertyChanged("HorizontalAlignment", ref _horizontalAlignment, value); }
        }

        /// <summary>
        ///     Called when the active mode has changed.
        /// </summary>
        /// <param name="previousMode">The previous mode.</param>
        /// <param name="newMode">The new mode.</param>
        protected override void OnModeChanged(Mode previousMode, Mode newMode)
        {
            if(previousMode == null)
                return;

            CopyModeProperties(previousMode, newMode, new[] {"Opacity"});
        }
    }
}